package com.vcredit.creditcat.web.app;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.ICustCreditRepositoryManager;
import com.vcredit.creditcat.api.ICustProductManager;
import com.vcredit.creditcat.dto.creditrepository.AuthItemDto;
import com.vcredit.creditcat.dto.creditrepository.AuthItemFilterDto;
import com.vcredit.creditcat.dto.creditrepository.AuthItemFilterParamDto;
import com.vcredit.creditcat.dto.creditrepository.CustCreditRepositoryDto;
import com.vcredit.creditcat.dto.creditrepository.ItemStatusDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.vo.AuthItemFilterVo;
import com.vcredit.creditcat.vo.CustCreditRepositoryVo;
import com.vcredit.creditcat.vo.ItemStatus;
import com.vcredit.creditcat.vo.ItemStatusListInputVo;
import com.vcredit.creditcat.vo.ItemStatusListOutputVo;

/**
 * 信用库相关
 *
 */
@RestController
@RequestMapping("/CreditRepository")
public class CustCreditRepositoryController extends BaseCreditCatController {

	@Reference(version = "1.0")
	private ICustCreditRepositoryManager custCreditRepositoryManager;

	@Reference(version = "1.0")
	private ICustProductManager custProductManager;

	/***
	 * 获取用户信用库信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMyCreditRepository")
	public CustCreditRepositoryVo getMyCreditRepository(HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);
		CustCreditRepositoryDto ret = custCreditRepositoryManager.getCustCreditRepository(cust.getLastProccessId());
		CustCreditRepositoryVo result = new CustCreditRepositoryVo();
		BeanUtils.copyProperties(ret, result);
		// 可以申请的产品
		//List<MerchantProductDto> list = custCreditRepositoryManager.queryCanApplyProducts(cust.getCustId());
		//result.setAllowApplyQty(Utility.isNullOrEmpty(list) ? 0 : list.size());
		return result;
	}

	/**
	 * 根据特定任务或目标，返回用户需完成的材料
	 * @param param
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getAuthItems")
	public AuthItemFilterVo getAuthItems(@RequestBody AuthItemFilterParamDto param, HttpServletRequest request) {
		Cust cust = super.getCust(request);
		AuthItemFilterDto ret = custCreditRepositoryManager.getAuthItems(param, cust.getLastProccessId());
		AuthItemFilterVo result = new AuthItemFilterVo();
		BeanUtils.copyProperties(ret, result);
		return result;
	}

	/***
	 * 查询认证项状态或重置认证项状态
	 * @param param
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStatus")
	public ItemStatus getStatus(@RequestBody AuthItemDto param, HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);
		ItemStatusDto ret = custCreditRepositoryManager.getStatus(param.getFrontId(), param.getFlag(), cust);
		ItemStatus result = new ItemStatus();
		BeanUtils.copyProperties(ret, result);
		return result;
	}
	
	/***
	 * 批量查询认证项状态或重置认证项状态
	 * @param param
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/batchGetStatus")
	public ItemStatusListOutputVo batchGetStatus(@RequestBody @Valid ItemStatusListInputVo param, HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);
		List<ItemStatus> list = new ArrayList<ItemStatus>();
		for (String itemCode : param.getItemCodes().split(",")) {
			ItemStatusDto ret = custCreditRepositoryManager.getStatus(itemCode, param.getFlag(), cust);
			ItemStatus result = new ItemStatus();
			BeanUtils.copyProperties(ret, result);
			list.add(result);
		}
		return new ItemStatusListOutputVo(list);
	}
}
